package com.emapp.lccode.activity

import android.content.ClipData
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.emapp.lccode.R
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.databinding.ActivityErrorBinding
import com.emapp.lccode.utils.showMessage
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.util.clipboardManager
import me.hgj.jetpackmvvm.ext.view.clickNoRepeat


class ErrorActivity : BaseActivity1<BaseViewModel, ActivityErrorBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "错误日志"
        mViewBind.lvTitle.tvRight.visibility = View.GONE
        mViewBind.lvTitle.ivLeft.visibility = View.GONE
        val config = CustomActivityOnCrash.getConfigFromIntent(intent)
        mViewBind.errorRestart.clickNoRepeat {
            config?.run {
                CustomActivityOnCrash.restartApplication(this@ErrorActivity, this)
            }
        }
        mViewBind.errorSendError.clickNoRepeat {
            CustomActivityOnCrash.getStackTraceFromIntent(intent)?.let {
                showMessage(it, "发现有Bug不去打作者脸？", "必须打", {
                    val mClipData = ClipData.newPlainText("errorLog", it)
                    // 将ClipData内容放到系统剪贴板里。
                    clipboardManager?.setPrimaryClip(mClipData)
                    Toaster.show("已复制错误日志")
                    try {
                        val url = "mqqwpa://im/chat?chat_type=wpa&uin=690810892"
                        startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
                    } catch (e: Exception) {
                        Toaster.show("请先安装QQ")
                    }
                }, "我不敢")
            }


        }
    }
}